package org.miaosha.controller;

import org.miaosha.model.Test;
import org.miaosha.redis.RedisService;
import org.miaosha.redis.TestKey;
import org.miaosha.result.Result;
import org.miaosha.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Wrb
 * @date 2019/7/10 10:32
 */
@RestController
@RequestMapping("/test")
public class TestController {

	@Autowired
	private TestService testService;

	@Autowired
	private RedisService redisService;

	@GetMapping("/db/get")
	public Result<Test> testDB() {
		Test test = testService.selectTestByKey(1);
		return Result.success(test);
	}

	@GetMapping("/db/tx")
	public Result<Integer> testTx() {
		testService.insertTestTx();
		return Result.success(1);
	}

	@GetMapping("/redis/set")
	public Result<Boolean> testRedisSet() {
		Test test = testService.selectTestByKey(1);

		return Result.success(redisService.set(TestKey.getById, "" + test.getId(), test));
	}

	@GetMapping("/redis/get")
	public Result<Test> testRedisGet() {
		Test test = redisService.get(TestKey.getById, "" + 1, Test.class);
		return Result.success(test);
	}
}
